package com.fd.servlet;

import com.fd.service.IStudentService;
import com.fd.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DeleteServlet extends HttpServlet {
//    @Autowired
//    private StudentService studentService;
//    StudentService studentService = new StudentService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentService studentService = getiStudentService();
        System.out.println(req.getParameter("id"));
        Integer id = Integer.valueOf(req.getParameter("id")) ;
        System.out.println(id);
        studentService.del(id);
        req.setAttribute("student", studentService.getAll());
        req.getRequestDispatcher("WEB-INF/html/data.jsp").forward(req, resp);
    }

    private static StudentService getiStudentService() {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext-jdbc-ann.xml");
        StudentService studentService = classPathXmlApplicationContext.getBean(StudentService.class);
        return studentService;
    }
}